package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhiqianbao.R;
import com.zhiqianbao.adapter.FamilyLinkmanList2Adapter;
import com.zhiqianbao.entity.ContaciInfo;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.trinea.android.common.util.StringUtils;

/**
 * Created by 子龙 on 2017/4/24.
 *
 * 联系人列表
 */

public class ContactInfo1Activity extends BaseActivity implements View.OnClickListener {
    public static final int DATA_FILDED=0x123;
    public static final int DATA_SCUESSFUL=0x124;
    private Gson gson = new Gson();
    private TextView TextView_add_contact;
    private RadioButton marray_cheack;
    private RadioButton marray_no;
    private ListView listView_family_linkman;
    private Button button_ruturn;
    private FamilyLinkmanList2Adapter familyLinkmanList2Adapter;
    private List<ContaciInfo.DataBean.UserContactsBean> userContactses1;
    private String marriage ;
    private String relation;
    private String relationshape="";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_household_contacts);
        initView();
        addListner();
        getContactinfo();
    }

    private void getContactinfo() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message message = new Message();
                Map<String, String> mapParams = new HashMap<>();
                mapParams.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                mapParams.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                String result = HttpUtils.httpPostString(Constants.API_ROOT + "/user/userContacts2",
                        mapParams, ContactInfo1Activity.this);
                if (StringUtils.isBlank(result)) {
                    message.what = DATA_FILDED;
                } else {
                    message.what = DATA_SCUESSFUL;
                    ContaciInfo data =gson.fromJson(result, ContaciInfo.class);
                    message.obj = data;
                }
                postHandler.sendMessage(message);
            }
        }).start();
    }

    private void addListner() {
        TextView_add_contact.setOnClickListener(this);
        marray_no.setOnClickListener(this);
        marray_cheack.setOnClickListener(this);
        button_ruturn.setOnClickListener(this);
    }

    private void initView() {
        //添加按钮
        TextView_add_contact = (TextView) findViewById(R.id.TextView_add_contact);
        marray_cheack = (RadioButton) findViewById(R.id.marray_cheack);
        marray_no = (RadioButton) findViewById(R.id.marray_no);
        listView_family_linkman = (ListView) findViewById(R.id.listView_family_linkman);
        button_ruturn = (Button) findViewById(R.id.button_ruturn);

    }

    @Override
    public void onClick(View v) {
        if(R.id.TextView_add_contact==v.getId()) {
            //添加
            Intent intent = new Intent(ContactInfo1Activity.this, AddContactsInfo2Activity.class);
            intent.putExtra("Marriger", marriage);
            intent.putExtra("relationshape", relationshape);
            startActivityForResult(intent, 1);
        }
        else if(R.id.button_ruturn==v.getId()){
                finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode){
            case 1:
                relationshape ="";
                getContactinfo();
                break;
        }
    }

    Handler postHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DATA_FILDED:
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case DATA_SCUESSFUL:
                    ContaciInfo response = (ContaciInfo) msg.obj;
                    if (response == null){
                        showShortToast1("网络超时，请再试!");
                        closeWaitDialog();
                    }
                    if (response!=null){
                        if (response.getCode().equals("0")) {
                            Log.i("asss", response.getData().getUserContacts().toString());
                            marriage = response.getData().getMarriage();
                            userContactses1 = new ArrayList<>();
                            List<ContaciInfo.DataBean.UserContactsBean> userContacts = response.getData().getUserContacts();
                            userContactses1.addAll(userContacts);
//                        showShortToast1(response.getMsg());
                            familyLinkmanList2Adapter = new FamilyLinkmanList2Adapter(ContactInfo1Activity.this, userContactses1);
                            listView_family_linkman.setAdapter(familyLinkmanList2Adapter);
                            Set<String> st = new HashSet<String>();
                            for(int i = 0 ; i < userContactses1.size() ; i++) {
                                ContaciInfo.DataBean.UserContactsBean userContactsBean = userContactses1.get(i);
                                relation = userContactsBean.getRelation();

                                if (relation.equals("母亲")){
//                                relationshape = relationshape+"母亲,";
                                    st.add(relation);
                                }
                                if (relation.equals("父亲")){
                                    st.add(relation);
//                                relationshape = relationshape+"父亲,";

                                }
                            }
                            if (st.size()!=0){
                                for (String str : st) {
                                    relationshape = str+","+relationshape;

                                }
                            }
                        }


                        if (response.getData().getMarriage() != null) {
                            if (response.getData().getMarriage().equals("2")) {
                                //2.已婚
                                marray_cheack.setChecked(true);
                                marray_no.setChecked(false);
                                marray_no.setEnabled(false);
                            } else if (response.getData().getMarriage().equals("0")) {
                                //0.未知(没填)
                                marray_cheack.setChecked(false);
                                marray_cheack.setChecked(false);
                                marray_cheack.setEnabled(false);
                            } else {
                                //未婚
                                marray_no.setChecked(true);
                                marray_cheack.setChecked(false);
                                marray_cheack.setEnabled(false);
                            }
                        } else {
                            showShortToast1(response.getMsg());
                        }
                    }
                    break;
            }

        }
    };
}
